/**
 * Created by tanbingqin on 2019/12/26.
 */
import React, { Component, PropTypes } from 'react';
import CompactInput from 'components/compact-input'
import { Input, InputNumber } from 'antd';
import { messages, deepFullCopy } from "share/common";

class Amount extends Component {
  state = {
    value: null
  };

  componentDidMount() {
    const { value } = this.props;
    this.setValue(this.props.value);
  }

  componentWillReceiveProps(nextProps) {
    if('value' in nextProps) {
      this.setValue(nextProps.value)
    }
  }

  setValue = value => {
    this.setState({value});
  };

  renderValueComponent = () => {
    const { value } = this.state;
    const { disabled } = this.props;
    return <InputNumber
      value={value}
      placeholder={messages('common.please.input')/*请输入*/}
      max={1000000000000000}
      min={0}
      precision={2}
      step={0.01}
      disabled={disabled}
      onChange={this.onChangeValue}
    />
  };

  onChangeValue = value => {
    this.setState({ value }, this.dispatchOnChange);
  };

  dispatchOnChange = () => {
    const { value } = this.state;
    const { onChange } = this.props;
    onChange && onChange(value);
  };

  render() {
    const { disabled, value, customClass } = this.props;
    return (
      <div className={customClass}>
        <CompactInput>
          <Input disabled value="CNY"/>
          {this.renderValueComponent()}
        </CompactInput>
      </div>
    );
  }

  static propTypes = {
    onChange: PropTypes.func,
    disabled: PropTypes.bool,
    customClass: PropTypes.string
  };

  static defaultProps = {
    onChange: () => {},
    disabled: false
  };
}

export default Amount;
